from woniubook.utility import *
# 定义全局变量用于保存登录用户的id
login_id = -1

# 注册
def reg():
    from woniubook.menu import start_menu
    username = input("[注册]请输入用户名：")
    pwd = input("[注册]请输入密码：")
    phone = input("[注册]请输入手机号码：")
    addr = input("[注册]请输入住址：")

    result = query_one("select username from customer where username = '%s'" % username)
    if result != None:
        print("用户名已存在")
    else:
        db_operation("insert into customer(username,pwd,phone,addr) values ('%s','%s','%s','%s')" % (username, pwd, phone,addr))
        print("注册成功")
    start_menu()

# 登录
def login():
    from woniubook.menu import start_menu
    from woniubook.menu import main_menu
    username = input("[登录]请输入用户名：")
    pwd = input("[登录]请输入密码：")
    result = query_one("select pwd from customer where username = '%s'" %username)
    if result != None:
        if result[0] == pwd:
            print("登录成功")
            # 修改全局变量为当前登录用户id
            global login_id
            login_id = query_one("select customerid from customer where username = '%s'" %username)
            main_menu()
        else:
            print("密码错误")
            start_menu()
    else:
        print("用户不存在")
        login()
